package com.yxzx.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxzx.api.dto.RestStatusCode;
import com.yxzx.api.entity.Category;
import com.yxzx.api.exception.CustomException;
import com.yxzx.api.service.ICategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author YWB
 * @since 2020-09-06
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    @GetMapping("/getCategorises")
    @ApiOperation("获取分类列表")
    public ResponseEntity<List<Category>> getCategoryList(){
        try{
            QueryWrapper<Category> queryWrapper=new QueryWrapper();

            //一级菜单
            queryWrapper.eq("deleted",0)
                    .eq("level",1)
                    .orderByAsc("sorted");

            List<Category> list = categoryService.list(queryWrapper);
            //二级科目

            for (Category category : list) {
                QueryWrapper<Category> query=new QueryWrapper<>();

                query.eq("deleted",0)
                        .eq("level",2)
                        .eq("parent_id",category.getId())
                        .orderByAsc("sorted");

                List<Category> subjectLists = categoryService.list(query);

                System.out.println(subjectLists);
                category.setSubjectList(subjectLists);


            }

            return ResponseEntity.ok(list);


        }catch(Exception e){
            throw  new CustomException("", RestStatusCode.CATEGORY_QUERY_ERROR.getCode(),RestStatusCode.CATEGORY_QUERY_ERROR.getMessage());
        }
    }



}
